package br.com.documentos.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import br.com.documentos.model.exceptions.UsuarioBloqueadoException;
import br.com.documentos.model.exceptions.UsuarioInativoException;

public class FalhaAutenticacao implements AuthenticationFailureHandler {
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, 
			AuthenticationException exception) throws IOException, ServletException {
		
		String erro;
		
		if( exception instanceof UsuarioBloqueadoException)	{
			erro = LoginMB.USUARIO_BLOQUEADO;
		}
		else if( exception instanceof UsuarioInativoException ) {
			erro = LoginMB.USUARIO_INATIVO;
		}
		else {
			erro = LoginMB.AUTENTICACAO;
		}

		response.sendRedirect(response.encodeRedirectURL("login.xhtml?id="+ erro));
	}

}
